package org.example.clouddemo.controller.app;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.clouddemo.bean.common.dto.ProductDTO;
import org.example.clouddemo.bean.common.entity.Product;
import org.example.clouddemo.bean.common.param.CreateProductParam;
import org.example.clouddemo.bean.common.param.GetDetailParam;
import org.example.clouddemo.bean.common.param.UpdateProductParam;
import org.example.clouddemo.bean.common.vo.ProductVO;
import org.example.clouddemo.service.common.ProductService;
import org.example.common.annotation.IgnoreAuth;
import org.example.common.response.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 产品表 内部接口
 * </p>
 *
 * @author hzg
 * @since 2025-06-07
 */

@Api(tags = "产品接口")
@ApiSupport(order = 1)
@RestController("appProductController")
@RequestMapping("/api/admin/product")
public class ProductController {

    @Autowired
    public ProductService productService;

    @ApiOperation(value = "产品列表", notes = "不需要token")
    @ApiOperationSupport(order = 1)
    @PostMapping("list/{page}/{pageSize}")
    @IgnoreAuth
    public ApiResult<IPage<ProductVO>> list(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize,@RequestBody(required = false) Map<String, Object> params) {
        Long productId = null;
        if (params != null && params.get("productId") != null) {
            productId = Long.valueOf(params.get("productId").toString());
        }

        if (productId != null) {
            // 推荐逻辑
            List<ProductVO> recommendProducts = productService.recommendProducts(productId);
            Page<ProductVO> pageInfo = new Page<>();
            pageInfo.setCurrent(page);
            pageInfo.setSize(pageSize);
            pageInfo.setTotal(recommendProducts.size());
            pageInfo.setRecords(recommendProducts);
            return ApiResult.success(pageInfo);
        } else {
            Page pageInfo = new Page<>();
            pageInfo.setCurrent(page);
            pageInfo.setSize(pageSize);
            IPage result = productService.selectPageVo(pageInfo);
            return ApiResult.success(result);

        }
    }

    @ApiOperation(value = "产品详情", notes = "不需要token")
    @ApiOperationSupport(order = 2)
    @PostMapping("query")
    @IgnoreAuth
    public ApiResult query(@Validated @RequestParam("productName") String productName) {
        Product product = new Product();
        product.setProductName(productName);

        return ApiResult.success(productService.selectOne(product));
    }

    @ApiOperation(value = "产品详情", notes = "不需要token")
    @ApiOperationSupport(order = 2)
    @PostMapping("queryOne")
    @IgnoreAuth
    public ApiResult queryOne(@Validated @RequestBody GetDetailParam param) {
        Product product = new Product();
        product.setId(param.getId());

        return ApiResult.success(productService.selectOne(product));
    }
    @ApiOperation(value = "创建产品", notes = "需要token")
    @ApiOperationSupport(order = 3)
    @PostMapping("create")
    public ApiResult create(@Validated @ModelAttribute CreateProductParam param, @RequestParam("file") MultipartFile file, HttpServletRequest request) {
        ProductDTO productDTO = BeanUtil.copyProperties(param, ProductDTO.class);
        return ApiResult.success(productService.create(productDTO,file,request));
    }

    @ApiOperation(value = "删除产品", notes = "需要token")
    @ApiOperationSupport(order = 3)
    @PostMapping("delete/{id}")
    public ApiResult delete(@PathVariable("id") String id, @RequestParam("flag") Boolean flag) {
        return ApiResult.success(productService.delete(id,flag));
    }

    @ApiOperation(value = "更新产品", notes = "需要token")
    @ApiOperationSupport(order = 4)
    @PostMapping("update")
    public ApiResult update(@ModelAttribute UpdateProductParam param,@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) {

        return ApiResult.success(productService.update(param,file,request));
    }

    @ApiOperation(value = "推荐商品", notes = "根据当前商品推荐相似商品")
    @ApiOperationSupport(order = 5)
    @PostMapping("recommend")
    public ApiResult<IPage<ProductVO>> recommend(@RequestParam("productId") Long productId) {
        List<ProductVO> recommendProducts = productService.recommendProducts(productId);
        Page pageInfo = new Page<>();
        pageInfo.setCurrent(1);
        pageInfo.setSize(recommendProducts.size());
        pageInfo.setRecords(recommendProducts);
        pageInfo.setTotal(recommendProducts.size());
        return ApiResult.success(pageInfo);
    }


}
